﻿using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

/* NOTE: make sure you change the deployment target from the Emulator to your Netduino before running this
 * Netduino sample app.  To do this, select "Project menu > AdvancedButtonApp Properties > .NET Micro Framework" and 
 * then change the Transport type to USB.   */

namespace mShield
{
    public class Program
    {
      
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        static OutputPort clk = new OutputPort(Pins.GPIO_PIN_D4, false);
        static OutputPort ds = new OutputPort(Pins.GPIO_PIN_D8, false);
        static OutputPort latch = new OutputPort(Pins.GPIO_PIN_D12, false);
        static OutputPort oe = new OutputPort(Pins.GPIO_PIN_D7, false);

        static string motorRegister;

        static PWM pwm = new PWM(Pins.GPIO_PIN_D5);
            

        public static void Main()
        {
            // write your code here
            InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);
            button.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

            motorRegister = GetIntBinaryString(2);
            Thread.Sleep(Timeout.Infinite);
        }

        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            forward();
            led.Write(data2 == 0);
        

            for (System.UInt32 i = 65; i < 105; i = i + 15 )
            {
                pwm.SetDutyCycle(i);
                Debug.Print(i.ToString());
                Thread.Sleep(2000);
            }
            pwm.SetDutyCycle(0);

            Thread.Sleep(5000);

            reverse();
            led.Write(data2 == 0);

            
            for (System.UInt32 i = 65; i < 105; i = i + 5)
            {
                pwm.SetDutyCycle(i);
                Debug.Print(i.ToString());
                Thread.Sleep(2000);
            }
            pwm.SetDutyCycle(0);

        }

        static void forward()
        {
            
            // write one 
            ds.Write(true);
            clockPulse();

            //shift over 6
            for (int i = 0; i <= 5; i++)
            {
                ds.Write(false);
                clockPulse();
            }

            latch.Write(true);
            latch.Write(false);

            oe.Write(true);
            oe.Write(false);

        }

        static void reverse()
        {
                        
            //shift over 7
            for (int i = 0; i <= 6; i++)
            {
                ds.Write(false);
                clockPulse();
            }

            // write one 
            ds.Write(true);
            clockPulse();

            latch.Write(1 == 0);

            oe.Write(1 == 0);

        }

        static void clockPulse()
        {
            clk.Write(1 == 0);
        }

        static string GetIntBinaryString(int n)
        {
            char[] b = new char[8];
            int pos = 7;
            int i = 0;

            while (i < 8)
            {
                if ((n & (1 << i)) != 0)
                {
                    b[pos] = '1';
                }
                else
                {
                    b[pos] = '0';
                }
                pos--;
                i++;
            }
            return new string(b);
        }



    }
}